package com.cnsoft.player.h5.ques;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.cnsoft.framework.db.page.PageModel;
import org.cnsoft.framework.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cnsoft.MyWxPLayerPageControllerSupport;
import com.cnsoft.persistent.communal2.R713010Ques.R713010QuesDBO;
import com.cnsoft.persistent.player2.U104060QuesVal.U104060QuesValDBO;

/**
 * 问卷功能
 * 
 * @author Zmsoft
 * @version 0.1.0 2018/4/2
 * @since 0.1.0 2018/4/2
 */
@Controller
@RequestMapping(value = "/player/1.0/h5/ques")
public class WXPlayerQuesController extends MyWxPLayerPageControllerSupport {

	@Resource
	WXPlayerQuesBusinesslogic myWXPlayerQuesBusinesslogic;

	/**
	 * 打开页面
	 * 
	 * @param [menuId]
	 * @return ModelAndView
	 * @throws Exception
	 * @author Zmsoft
	 * @version 0.1.0 2018/4/2
	 * @since 0.1.0 2018/4/2
	 */
	@RequestMapping(value = "/forms.js", method = RequestMethod.POST)
	public ModelAndView loadFormVal(R713010QuesDBO param) throws Exception {
		ModelAndView modelAndView = getModelAndView("/weixinplayercommon/player-ques-data");

		// 读取问卷基本信息
		// 读取问卷题目信息
		// 读取问卷题目选项信息
		if(myWXPlayerQuesBusinesslogic.checkData(param)) {
//			modelAndView = getModelAndView("/weixinplayercommon/player-command-ok");
			modelAndView.addObject("message", "您已经成功使用过一次");
//			return modelAndView;
		}

		modelAndView.addAllObjects(myWXPlayerQuesBusinesslogic.loadData(param));

		return modelAndView;
	}
	
	@RequestMapping(value = "/forms.ok", method = RequestMethod.POST)
	public ModelAndView loadData(HttpServletRequest request, U104060QuesValDBO param) throws Exception {
		ModelAndView modelAndView = getModelAndView("/weixinplayercommon/player-command-ok");
		logger.debug("param=====>>>>" + param.toJsonString());

		//填写内容
		String qd = JSONObject.toJsonString(getParamDataMap(request,"qs"));
		logger.debug("param=====>>>>" + qd);
		param.setQuesDetail(qd);
		
		//数据保存
		myWXPlayerQuesBusinesslogic.saveData(param);

		modelAndView.addObject("message", "填报成功");
		return modelAndView;
	}

	// 一览（分页查询）
	@RequestMapping(value = "/index.js", method = RequestMethod.POST)
	public ModelAndView doIndex(R713010QuesDBO param, PageModel<R713010QuesDBO> pageModel) throws Exception {
		// 输出参数日志
		logger.debug("param=====>>>>" + param.toString());
		// 设定展示界面
		ModelAndView modelAndView = getModelAndView("/weixinplayercommon/player-ques-list");
		pageModel.setFormParamBean(param);
		// 开启记录数统计
		// pageModel.setResultCountFlag(true);
		// 排序模式
		// pageModel.setOrderbyCreateTimeDESC();
		// 分页查询
		myWXPlayerQuesBusinesslogic.doIndex(pageModel);
		// 设定返回
		modelAndView.addObject("page", pageModel);
		modelAndView.addObject("param", param);
		return modelAndView;
	}
}
